package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.OnNavigationListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import app.Disthanitian.thaistyle.EmerMez.ChangePassword.WebPageTask;
import app.Disthanitian.thaistyle.EmerMez.RegisterActivity.Runbg;

public class ForgetPassword extends Activity {
	
	EditText emailforgetpass ;
    static UrlEmerMez URLlink = new UrlEmerMez();
	
	public static final String urlForgetpass = URLlink.getUrlforgetpass();
	public static final String urlcheckmail = URLlink.getUrlcheckmail();
	public static String result = "";
	String email;
	String emailpass;
	MemberUser user = new MemberUser();

	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.forgetpassword);
		
		//emailforgetpass = (EditText) findViewById(R.id.editText_forgetpass);
		
		Button bt_forgetpass = (Button) findViewById(R.id.submit_forgetpass);
		 bt_forgetpass.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				
				//1)Check email Have Account
				Runbg a = new Runbg();
				a.execute();

			}
		});

	}
	
  //Checkemail Have Account
	
	public String checkemail() {
		
		emailforgetpass = (EditText) findViewById(R.id.editText_forgetpass);
		
	    email = emailforgetpass.getText().toString().trim();

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("email", email));

		return postDataCheck(urlcheckmail,nameValuePairs);

	}

	public String postDataCheck(String url,List<NameValuePair> nameValuePairs) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}
	
	
	
	public String ForgetPass() {
				
		emailforgetpass = (EditText) findViewById(R.id.editText_forgetpass);
		emailpass = emailforgetpass.getText().toString().trim();
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("email", emailpass));

		return postData(urlForgetpass, nameValuePairs);

	}
	
	public String postData(String url, List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}
	
	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
		
			result = ForgetPass();
			return result;
		}

		protected void onPostExecute(String result) {
			
			Toast.makeText(ForgetPassword.this,result,Toast.LENGTH_LONG).show();
			
			if (result.trim().equals("SendEmail Sending")) {
				Toast.makeText(ForgetPassword.this,"Send Password to Your Email!!",Toast.LENGTH_LONG).show();
			} else if (result.trim().equals("SendEmail Can Not Send")) {
				Toast.makeText(ForgetPassword.this, "Cannot Send Password to Your Email !!",Toast.LENGTH_LONG).show();
			} else if(result.trim().equals("Not send")){
				Toast.makeText(ForgetPassword.this, "Email is Not Account !!",Toast.LENGTH_LONG).show();
			}

		}

	}
	
	protected class Runbg extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			result = checkemail();

			return result;
		}

		protected void onPostExecute(String result) {
			if (result.trim().equals("email is not register")) {
				Toast.makeText(ForgetPassword.this, "email is not register ",
						Toast.LENGTH_LONG).show();
				// No Email have Account
			}else{
				// Have Account Send Email
				WebPageTask task = new WebPageTask();
				task.execute();
			}

		}

	}

}
